package de.openkanban.client.ui.presenter;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.smartgwt.client.widgets.AnimationCallback;

import de.openkanban.client.Main;
import de.openkanban.client.ui.board.BoardView;
import de.openkanban.client.util.RootPanel;
import de.openkanban.shared.dto.BoardDto;

public class SwitchBoardRequestCallback implements RequestCallback {

	private BoardView boardView;

	public SwitchBoardRequestCallback(BoardView boardView) {
		this.boardView = boardView;
	}

	@Override
	public void onError(Request request, Throwable exception) {

	}

	@Override
	public void onResponseReceived(Request request, final Response response) {
		boardView.animateFade(0, new AnimationCallback() {
			@Override
			public void execute(boolean earlyFinish) {
				BoardDto switchBoardDto = new BoardDto(response.getText());
				if (switchBoardDto.getBoardId() != null) {
					RootPanel.boardComponent.switchBoard(switchBoardDto.getBoardId());
				}

				boardView.animateFade(100, null, Main.DEFAULT_FADE_DURATION);
			}
		}, Main.DEFAULT_FADE_DURATION);
	}
}
